<?php

namespace App\Http\Controllers\Home;


use App\Models\Tel_user;
use Illuminate\Http\Request;

//使用类
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Validator;


//注册控制器 短信
class RegisterController extends Controller{


    /* 验证 注册 */
    public function index(Request $request){


            if($request->isMethod('POST')){
                //$arr = $request->except(['_token','code','yzm']);
                //dd($arr);
                $valArr = $request->all();
                $valArr['code'] = base64_decode($request->input('code'));


                        //Validator 类验证
                        $validator = Validator::make($valArr,[
                            'name' => 'required|max:8',
                            'pwd' => 'required|min:6',
                            'pwd_confirmation' => 'required|min:6|same:pwd',
                            'phone' => 'required|unique:tel_user|regex:/^1[34578][0-9]{9}$/',
                            'code'  => 'same:yzm',//生成赋值的验证码
                            'yzm'  => 'required|between:4,4', //输入的验证码
                        ],[
                            'required' => ':attribute为必填项',
                            'min' => ':attribute长度不符合要求',
                            'max' => ':attribute长度不符合要求',
                            'phone.unique' =>':attribute只能接收一次验证码',
                            'phone.regex' =>':attribute格式不对',
                            'pwd_confirmation.same' => ':attribute和密码不一样',
                            'between'=>'attribute长度不符合要求',
                            'code.same' => ':attribute输入错误',
                        ],[
                            'name'=>'用户名',
                            'pwd'=>'密码',
                            'pwd_confirmation' => '确认密码',
                            'phone'=>'手机号',
                            'code'=>'验证码',
                            'yzm'=>'验证码',
                        ]);

                        if($validator->fails()){//如果验证出错误
                            //$validator->getMessageBag()->getMessages()
                            //$json['msg'] = $name.'长度不能小于于'.$min;
                            return response() -> json($validator->getMessageBag()->first());
                        }


                        $arr = $request->only(['name','phone']);
                        $arr['pwd']= Crypt::encrypt($request->input('pwd'));//密码加密
                        $arr['status']=1;
                        $arr['create_time']=time();

                        if(Tel_user::insert($arr)){
                            return 1;
                        }else{
                            return 0;
                        }



            }



         return view("home.register");
    }

    /* 发短信 */
    public function smsyzm(Request $request){
        //dd($request->all());
        //载入ucpass类
        require_once("../app/common/sms/lib/Ucpaas.class.php");
        //初始化必填
        //填写在开发者控制台首页上的Account Sid
         $options['accountsid']='888d207b9327ce56360f80d6f48d52a6';
        //填写在开发者控制台首页上的Auth Token
         $options['token']='01a8790dd52461587f52b1506c172e0a';
        //初始化 $options必填
        $ucpass = new \Ucpaas($options);

        $appid = "c9ea440f35f74468a8845e89f2f9c4b1";	//应用的ID，可在开发者控制台内的短信产品下查看
        $templateid = "457651";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
        $yzm = base64_decode($request->input('yzm')); //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空   code进行解密
        $time = 5;
        $param="$yzm,$time";
        $mobile = $request->input('yzmtel');//$_POST['yzmtel']
        $uid = "";

        //70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。

        echo $ucpass->SendSms($appid,$templateid,$param,$mobile,$uid);

    }


    /*生成验证码*/
    public function code(){
        $code_len=4;
        $code=array_merge(range('A','Z'),range('a','z'),range(1,9));//需要用到的数字或字母
        $keyCode=array_rand($code,$code_len);//真正的验证码对应的$code的键值
        if($code_len==1){
            $keyCode=array($keyCode);
        }
        shuffle($keyCode);//打乱数组
        $verifyCode="";
        foreach ($keyCode as $key){
            $verifyCode.=$code[$key];//真正验证码
        }
        echo base64_encode($verifyCode); //加密
    }

}